﻿using System;
using System.Collections.Generic;
using System.Linq;

using btmerc.API.Enums;

namespace btmerc.API.Schemas
{
    // TODO: we want an interface for Troop
    public partial class Troop
    {
        public bool IsCombatTroop
        {
            get
            {
                return this.Type != TroopType.Administrative &&
                       this.Type != TroopType.Medical &&
                       this.Type != TroopType.BattlemechTech &&
                       this.Type != TroopType.VehicleMechanic;
            }
        }

        public ushort Pilot
        {
            get
            {
                if (!IsCombatTroop)
                {
                    return 10;
                }
                return this.Character.Select(c => c.Skills.GetSkill("Pilot").Value).Min();
            }
        }

        public ushort Gunnery
        {
            get
            {
                if (!IsCombatTroop)
                {
                    return 10;
                }
                return this.Character.Select(c => c.Skills.GetSkill("Gunnery").Value).Min();
            }
        }

        public float AverageExperience
        {
            get
            {
                return Character.Select(c => (float)(c.PrimarySkills.Sum(s => s.Value)) / c.PrimarySkills.Count).Average();
            }
        }

        public Quality AverageQuality
        {
            get
            {
                // get average experience rating of all combat units
                float aer = AverageExperience;
                if (aer <= 2.5)
                {
                    return Quality.Elite;
                }
                else if (aer <= 4)
                {
                    return Quality.Veteran;
                }
                else if (aer <= 5.5)
                {
                    return Quality.Regular;
                }
                else
                {
                    return Quality.Green;
                }
            }
        }

        public ushort MaintenanceTimeNeeded
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public void SetCharacterReferences()
        {
            foreach (ForceCharacter forceCharacter in Character)
            {
                forceCharacter.Troop = this;
            }
        }
    }
}
